package com.yundingai.service.impl;

import com.yundingai.domain.entity.RelStudyInfoSubject;
import com.yundingai.domain.entity.StudyInfo;
import com.yundingai.domain.entity.Subject;
import com.yundingai.domain.entity.vo.ReceiveSubjectListVo;
import com.yundingai.domain.entity.vo.StudyInfoVo;
import com.yundingai.mapper.StudyInfoMapper;
import com.yundingai.mapper.SubjectMapper;
import com.yundingai.service.StudyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StudyInfoServiceImpl implements StudyInfoService {
    @Autowired
    private StudyInfoMapper studyInfoMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    //insertStudyInfo这个方法表示初始用户第一次保存学习信息设置，所以是新增数据
    @Override
    public void insertStudyInfo(ReceiveSubjectListVo receiveSubjectListVo) {
        Integer userId = receiveSubjectListVo.getUserId();
        //新增学习信息表，
        studyInfoMapper.insertStudyInfo(receiveSubjectListVo);
        List<String> subjectList = receiveSubjectListVo.getFrontSubjectList();
        //遍历前端传进来的学科列表，把数据存到学科信息表，每遍历一次就新增一条学科信息
        for (String subjectName : subjectList) {
            Subject subject = new Subject();
            subject.setSubjectName(subjectName);
            subjectMapper.insertSubject(subject);

            Integer subjectId = subject.getSubjectId();
            //中间表插入数据
            studyInfoMapper.insertRelSubjectInfo(receiveSubjectListVo.getStudyInfoId(),subjectId);
//            StudyInfo studyInfo = studyInfoMapper.selectStudyInfoVoByUserId(userId);
        }
    }

    @Override
    public StudyInfo selectStudyInfoByUserId(Integer userId) {
        StudyInfo userId1 = studyInfoMapper.selectStudyInfoByUserId(userId);
        return userId1;
    }
//此方法表示用户后续点击保存学习信息设置时，触发更新方法，
    @Override
    public void updateStudyInfo(ReceiveSubjectListVo receiveSubjectListVo) {

        //更新学习信息表
        studyInfoMapper.updateStudyInfo(receiveSubjectListVo);
        //根据userId查询学习信息表中的studyinfo_id，，然后拿到studyinfo_id去查询中间表，
        // 获取subject_id，再通过subject_id查询学科表，最后，先删学科表，再删中间表，顺序必须按照这个来
        //selectStudyInfoVoByUserId直接使用这个方法，因为前端已经使用这个方法获取到了userid的值，直接使用就行
        List<String> frontSubjectList = receiveSubjectListVo.getFrontSubjectList();
        Integer userId = receiveSubjectListVo.getUserId();
        StudyInfo studyInfo1 = studyInfoMapper.selectStudyInfoVoByUserId(userId);
        Integer studyInfoId = studyInfo1.getStudyInfoId();
        List<RelStudyInfoSubject> relStudyInfoSubjects = studyInfoMapper.selectRelSubjectInfoByInfoId(studyInfoId);
        for(RelStudyInfoSubject relStudyInfoSubject : relStudyInfoSubjects){
            //根据subject_id查询学科表
            Subject subject = studyInfoMapper.selectSubjectBySubjectId(relStudyInfoSubject.getSubjectId());
            studyInfoMapper.deleteSubject(subject.getSubjectId());
            studyInfoMapper.deleteRelSubjectInfo(relStudyInfoSubject.getStudyInfoId());
        }
           //遍历frontSubjectList
            for (String subjectName :frontSubjectList){
               //通过userid查到的subject_name，删除掉之后，，直接通过添加遍历的数据
                Subject subject1 = new Subject();
                subject1.setSubjectName(subjectName);
                studyInfoMapper.insertSubject(subject1);


                Integer subjectId = subject1.getSubjectId();
                //新增中间表
                studyInfoMapper.insertRelSubjectInfo(studyInfoId,subjectId);
            }





    }

    @Override
    public StudyInfo selectStudyInfoVoByUserId(Integer userId) {
        StudyInfo studyInfo = studyInfoMapper.selectStudyInfoVoByUserId(userId);
        return studyInfo;
    }
}
